/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin		 	*/
/*	Project:	GoLib-1.0.0			*/
/*	Author:		gong_libin			*/
/*	Date:		2016_01_01			*/
/*	File:		GoFile.go			*/
/********************************************************/

package GoCls

import (
	"os"
	"bufio"
)

const (
	GO_FL_EOF	string = "EOF"
)

const (
	GO_FL_RDWR	int = os.O_RDWR
	GO_FL_EXCL	int = os.O_EXCL
	GO_FL_RDOL	int = os.O_RDONLY
	GO_FL_WROL	int = os.O_WRONLY
	GO_FL_APPD	int = os.O_APPEND
	GO_FL_CRTE	int = os.O_CREATE
)

const (
	GO_FL_SSET	int = os.SEEK_SET
	GO_FL_SCUR	int = os.SEEK_CUR
	GO_FL_SEND	int = os.SEEK_END
)

// GoFile struct
type GoFile struct {
	file	*os.File
}

func (f *GoFile) GoFileClose() {
	f.file.Close()
}

func (f *GoFile) GoFileSize() int64 {
	info, _ := f.file.Stat()
	return info.Size()
}

func (f *GoFile) GoFileGet() *os.File {
	return f.file
}

func (f *GoFile) GoFilePut(in *os.File) {
	f.file = in
}

func (f *GoFile) GoFileDelete(name string) error {
	return os.Remove(name)
}

func (f *GoFile) GoFileCreate(name string) (err error) {
	f.file, err = os.Create(name)
	return err
}

func (f *GoFile) GoFileRead(buf []byte) (int, error) {
	return f.file.Read(buf)
}

func (f *GoFile) GoFileWrite(buf []byte) (int, error) {
	return f.file.Write(buf)
}

func (f *GoFile) GoFileOpen(name string, flag int) (err error) {
	f.file, err = os.OpenFile(name, flag, 0666)
	return err
}

func (f *GoFile) GoFileSeek(offset int64, whence int) (int64, error) {
	return f.file.Seek(offset, whence)
}

// GoLog struct
type GoLog struct {
	GoFile	// Parent
	bio	*bufio.Reader
}

func (l *GoLog) GoLogBufio() {
	l.bio = bufio.NewReader(l.file)
}

func (l *GoLog) GoLogLine(delim byte) (line string, info string) {
	line, errs := l.bio.ReadString(delim)
	if nil != errs {
		info = errs.Error()
	}
	return line, info
}
